package com.touringapp.android.utils;

import com.touringapp.android.utils.constants.Constants;

import android.util.Log;

/**
 * Logger wrapper to check logging level before logging
 * 
 * @author Andrey Chorniy
 */
public class Logger {
	private static final String DELIMETER = " ";
	
	/**
	 * Log with debug level logging
	 * @param messageParts set of objectes which will be converted with obj.toString() and concatenated with DELIMETER
	 */
	public static void debug(Object ... messageParts) {
		if (Log.isLoggable(Constants.LOG_TAG, Log.DEBUG)) {
			Log.d(Constants.LOG_TAG, buildString(DELIMETER, messageParts));
		}
	}

	public static void i(Object ... messageParts) {
		if (Log.isLoggable(Constants.LOG_TAG, Log.INFO)) {
			Log.i(Constants.LOG_TAG, buildString(DELIMETER, messageParts));
		}
	}
	public static void i(Throwable tr, Object ... messageParts) {
		if (Log.isLoggable(Constants.LOG_TAG, Log.INFO)) {
			Log.i(Constants.LOG_TAG, buildString(DELIMETER, messageParts), tr);
		}
	}
	
	public static void e(Object ... messageParts) {
		if (Log.isLoggable(Constants.LOG_TAG, Log.ERROR)) {
			Log.e(Constants.LOG_TAG, buildString(DELIMETER, messageParts));
		}
	}

	public static void e(Throwable tr, Object ... messageParts) {
		if (Log.isLoggable(Constants.LOG_TAG, Log.ERROR)) {
			Log.e(Constants.LOG_TAG, buildString(DELIMETER, messageParts), tr);
		}
	}

	public static void v(Object ... messageParts) {
		if (Log.isLoggable(Constants.LOG_TAG, Log.VERBOSE)) {
			Log.v(Constants.LOG_TAG, buildString(DELIMETER, messageParts));
		}
	}

	public static void w(Object ... messageParts) {
		if (Log.isLoggable(Constants.LOG_TAG, Log.WARN)) {
			Log.w(Constants.LOG_TAG, buildString(DELIMETER, messageParts));
		}
	}
	public static void w(Throwable tr, Object ... messageParts) {
		if (Log.isLoggable(Constants.LOG_TAG, Log.WARN)) {
			Log.w(Constants.LOG_TAG, buildString(DELIMETER, messageParts),tr);
		}
	}
	
	public static void d(String tag, String msg) {
		if (Log.isLoggable(tag, Log.DEBUG)) {
			Log.d(tag, msg);
		}
	}

	public static void d(String msg) {
		d(Constants.LOG_TAG, msg);
	}

	
	public static void i(String tag, String msg) {
		if (Log.isLoggable(tag, Log.INFO)) {
			Log.i(tag, msg);
		}
	}

	public static void e(String tag, String msg) {
		if (Log.isLoggable(tag, Log.ERROR)) {
			Log.e(tag, msg);
		}
	}

	public static void e(String tag, String msg, Throwable tr) {
		if (Log.isLoggable(tag, Log.ERROR)) {
			Log.e(tag, msg, tr);
		}
	}

	public static void v(String tag, String msg) {
		if (Log.isLoggable(tag, Log.VERBOSE)) {
			Log.v(tag, msg);
		}
	}

	public static void w(String tag, String msg) {
		if (Log.isLoggable(tag, Log.WARN)) {
			Log.w(tag, msg);
		}
	}
	
	public static final String buildString (final String delimiter, final Object ... concatenatedParts){
		int totalLength = delimiter.length()*concatenatedParts.length;
		for (int i = 0; i < concatenatedParts.length; i++) {
			if (concatenatedParts[i] != null){
				totalLength += concatenatedParts[i].toString().length();
			}
		}
		final StringBuilder sb = new StringBuilder(totalLength);

		for (int i = 0; i < concatenatedParts.length; i++) {
			Object part = concatenatedParts[i];
			if (i > 0){
				sb.append(delimiter);
			}
			sb.append(part);
		}
		return sb.toString();
	}
}